package cn.toys.modular.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.toys.base.auth.context.LoginContextHolder;
import cn.toys.base.auth.model.LoginUser;
import cn.toys.modular.entity.ProductAttr;
import cn.toys.modular.entity.ProductAttrExample;
import cn.toys.modular.entity.ProductType;
import cn.toys.modular.entity.ProductTypeExample;
import cn.toys.modular.mapper.ProductAttrMapper;
import cn.toys.modular.service.IProductAttrService;
import cn.toys.sys.core.constant.factory.ConstantFactory;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品属性表 服务实现类
 * </p>
 *
 * @author toys
 * @since 2021-02-26
 */
@Service
public class ProductAttrServiceImpl implements IProductAttrService {

    @Autowired
    private ProductAttrMapper productAttrMapper;

    @Override
    public ProductAttr selectById(Integer id) {
        ProductAttrExample productAttrExample = new ProductAttrExample();
        productAttrExample.createCriteria().andIdEqualTo(id.longValue());
        return productAttrMapper.selectOneByExample(productAttrExample);
    }

    @Override
    public Long getCountByName(String name) {
        ProductAttrExample productAttrExample = new ProductAttrExample();
        if(StringUtils.isNotEmpty(name)){
            productAttrExample.createCriteria().andNameLike(name);
        }
        Long count = productAttrMapper.countByExample(productAttrExample);
        return count;
    }

    @Override
    public List selectByName(String name) {
        ProductAttrExample productAttrExample = new ProductAttrExample();
        if(StringUtils.isNotEmpty(name)){
            productAttrExample.createCriteria().andNameLike(name);
        }
        List<ProductAttr> productAttrs = productAttrMapper.selectByExample(productAttrExample);
        if(productAttrs != null && productAttrs.size()>0){
            List<Long> createUserIds = productAttrs.stream().map(ProductAttr::getCreateUserId).collect(Collectors.toList());
            List<Long> updateUserIds = productAttrs.stream().map(ProductAttr::getUpdateUserId).collect(Collectors.toList());
            createUserIds.addAll(updateUserIds);
            Map<Long,String> userMap = ConstantFactory.me().getUserNameMap(createUserIds);
            for(ProductAttr productAttr: productAttrs){
                if(userMap.containsKey(productAttr.getCreateUserId())){
                    String createUserName = userMap.get(productAttr.getCreateUserId());
                    productAttr.setCreateUserName(createUserName);
                }

                if(userMap.containsKey(productAttr.getUpdateUserId())){
                    String updateUserName = userMap.get(productAttr.getUpdateUserId());
                    productAttr.setUpdateUserName(updateUserName);
                }
            }
        }
        return productAttrs;
    }

    @Override
    public Integer insert(ProductAttr productAttr) {
        LoginUser user = LoginContextHolder.getContext().getUser();
        if(productAttr != null){
            Date current = new Date();
            productAttr.setUpdateTime(current);
            productAttr.setUpdateUserId(user.getId());
            productAttr.setCreateTime(current);
            productAttr.setCreateUserId(user.getId());
            //todo 多租户ID
            productAttr.setTenantId(1L);
        }
        return productAttrMapper.insert(productAttr);
    }

    @Override
    public Integer deleteById(Integer productAttrId) {
        ProductAttrExample productAttrExample = new ProductAttrExample();
        productAttrExample.createCriteria().andIdEqualTo(productAttrId.longValue());
        return productAttrMapper.deleteByExample(productAttrExample);
    }

    @Override
    public Integer update(ProductAttr productAttr) {
        if (ToolUtil.isOneEmpty(productAttr, productAttr.getId(), productAttr.getName(), productAttr.getAttrContext())) {
            throw new RequestEmptyException();
        }
        LoginUser user = LoginContextHolder.getContext().getUser();
        Date current = new Date();
        productAttr.setUpdateTime(current);
        productAttr.setUpdateUserId(user.getId());

        ProductAttrExample productAttrExample = new ProductAttrExample();
        productAttrExample.createCriteria().andIdEqualTo(productAttr.getId());
        ProductAttr old = productAttrMapper.selectOneByExample(productAttrExample);
        BeanUtil.copyProperties(productAttr, old);
        return productAttrMapper.updateByExampleSelective(old,productAttrExample);
    }


}
